/*
 * @Author: HZH
 * @Date: 2022-04-12 14:23:45
 * @LastEditors: HZH
 * @LastEditTime: 2022-10-18 15:27:44
 * @Description: 
 */
const mysql = require('mysql')
module.exports = {
    //数据库配置
    config:{
        host:'120.79.217.114',
        port:'3306',
        user:'root',
        password:'123456',
        database:'staff_sys',
    },
    // 连接数据库，使用连接池
    // 连接池对象
    sqlConnection:function(sql,sqlArr,callBack){
        var pool = mysql.createPool(this.config)
        pool.getConnection((err,conn) => {
            console.log('123456');
            if(err){
                console.log('连接失败了');
                return
            }
            // 事件驱动回调
            conn.query(sql,sqlArr,callBack);
            conn.release();
        })
    },

    sql_connect_async: function (sysql, sql_arr) {
        return new Promise((resolve, reject) => {
          var pool = mysql.createPool(this.config);
          pool.getConnection((err, conn) => {
            if (err) return reject(err)
            else {
              conn.query(sysql, sql_arr, (err, data) => {
                if (err) {
                  reject(err)
                }
                else {
                  resolve(data)
                }
              })
              conn.release();  // 释放连接
            }
          })
        }).catch(err => {
          console.log(err + 'sql_connect_async函数出错')
        })
      }
    
}